package com.gsdse.service.impl;


import com.aliyuncs.exceptions.ClientException;
import com.gsdse.dao.UserDao;
import com.gsdse.pojo.ReturnData;
import com.gsdse.pojo.User;
import com.gsdse.service.UserService;
import com.gsdse.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


/**
 * DevicesInfo serviceimpl
 *
 * @Author jhfuture
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public Boolean InsertUser(User user) {
        Boolean a = userDao.InsertUser(user);
        return a;
    }

    @Override
    public Boolean updateUserById(User user) {
        Boolean aBoolean = userDao.updateUserById(user);
        return aBoolean;
    }


    @Override
    public User selectUserByUserId(Integer userId) {
        User user = userDao.selectUserByUserId(userId);
        return user;

    }

    @Override
    public Boolean updateUserAndAvatarById(User user) {

        return userDao.updateUserAndAvatarById(user);
    }

    @Override
    public ReturnData checkHaveEdit(Integer userId) {
        ReturnData returnData = new ReturnData();
        if(userId==null)
        {
            returnData.setCode(2);
            returnData.setMsg("id为空");
            return returnData;
        }
        User user = userDao.selectUserByUserId(userId);
        if(user==null || user.getEmail().isEmpty()||user.getPhoneNumber().isEmpty()||"新用户".equals(user.getUserName())){
            returnData.setCode(0);
            returnData.setMsg("检测成功");
            returnData.setData(false);
        }
        else{
            returnData.setCode(0);
            returnData.setMsg("检测成功");
            returnData.setData(true);
        }
        return returnData;
    }

    @Override
    public User selectUserByReallyNameAndPhoneNumber( String userName, String phoneNumber) {
        User user = userDao.selectUserByReallyNameAndPhoneNumber(userName,phoneNumber);
        return user;
   }


    @Override
    public User selectUserByReallyNameAndEmail(  String userName,  String email) {
        User user = userDao.selectUserByReallyNameAndEmail(userName,email);
        return user;
    }

    @Override
    public ReturnData uploadAvatar(MultipartFile file) throws ClientException, IOException {

        //注意!!此处修改，对应的发送邮箱代码的获取图片路径也需要更改
        String uploadPath = "/usr/local/src/avatar";
        //String uploadPath = "D:\\KJpic\\avatar";

        //String onlinePath = "https://studynest.top/avatar/";
        //String onlinePath = "https://gsdse.cn/avatar/";

        String originalFilename = file.getOriginalFilename();
        String suffixd = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = new Date().getTime() + "_" + new Random().nextInt(1000) + suffixd;

        String strDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File haveExist = new File(uploadPath + '/' + strDate);
        String OnlinePath = AliOSSUtils.uploadFile(file,strDate+"/"+newFileName,"avatar" );
        System.out.println(OnlinePath);
        if (!haveExist.exists()) {
            haveExist.mkdirs();
        }

        File uploadFile = new File(haveExist, newFileName);


        ReturnData returnData = new ReturnData();
        try {
            file.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("上传文件错误！");
            returnData.setCode(1);
            returnData.setMsg("上传文件失败！");
            return  returnData;
        }


        returnData.setCode(0);
        returnData.setMsg("上传文件成功！");
        returnData.setData(OnlinePath);
        return  returnData;

    }


}

